<?php

namespace App\Http\Controllers;

use App\Models\Constant\Errors;
use App\Models\Error;

class IndexController extends Controller
{
    public function getList($model,$per){
        $data = $model::getPageData($per);
        $err = new Error();
        if($data->isEmpty()){
            $err->setError(Errors::NOT_FOUND);
            $err->setData('没有查询到任何数据');
            return response()->json($err);
        }
        $err->setData($data);
        return response()->json($err);
    }

    public function show($id,$model){
        $data = $model::getOne($id);
        $err = new Error();
        if(is_null($data)){
            $err->setError(Errors::NOT_FOUND);
            $err->setData('没有该记录');
        }else{
            $err->setData($data);
        }
        return response()->json($err);
    }

    public function create($request,$model){
        $data = $request->input();
        $err = new Error();
        try{
            if(count($data) == 0){
                $err->setError(Errors::FAILED);
                $err->setData('请添加数据');
                return response()->json($err);
            }
            $model::addData($data);
            $err->setError(Errors::COK);
            $err->setData('添加成功');
        }catch (\Exception $e){
            dd($e->getMessage());
            $err->setError(Errors::FAILED);
            $err->setData('添加失败');
        }
        return response()->json($err);
    }

    public function update($request,$id,$model){
        $data = $request->input();
        $err = new Error();
        try{
            if(count($data) == 0){
                $err->setError(Errors::FAILED);
                $err->setData('请输入修改数据');
                return response()->json($err);
            }
            if(is_null($model::find($id))){
                $err->setError(Errors::NOT_FOUND);
                $err->setData('没有该记录');
                return response()->json($err);
            }
            $model::updateData($data,$id);
            $err->setError(Errors::UOK);
            $err->setData('修改成功');
        }catch (\Exception $e){
            dd($e->getMessage());
            $err->setError(Errors::FAILED);
            $err->setData('修改失败');
        }
        return response()->json($err);
    }

    public function delete($request,$model){
        //dd($model::withTrashed()->restore());
        $ids = $request->input('ids');
        $err = new Error();
        if(is_null($ids) || !isset($ids)){
            $err->setError(Errors::FAILED);
            $err->setData('数据格式错误');
            return response()->json($err);
        }
        $ids = explode(',', $ids);
        foreach ($ids as $k=>$id){
            $ids[$k] = (int) $id;
        }
        try{
            $model::del($ids);
            $err->setError(Errors::DOK);
            $err->setData('删除成功');
        }catch (\Exception $e){
            // dd($e->getMessage());
            $err->setError(Errors::FAILED);
            $err->setData('删除失败');
        }
        return response()->json($err);
    }

    public function recover($request,$model){
        $ids = $request->input('ids');
        $err = new Error();
        if(is_null($ids) || !is_array($ids)){
            $err->setError(Errors::FAILED);
            $err->setData('数据格式错误');
            return response()->json($err);
        }
        foreach ($ids as $k=>$id){
            $ids[$k] = (int) $id;
        }
        try{
            $model::recover($ids);
            $err->setData('恢复成功');
        }catch (\Exception $e){
            //$res = $e->getMessage();
            $err->setError(Errors::FAILED);
            $err->setData('恢复失败');
        }
        return response()->json($err);
    }

    public function getByCond($condition,$model){
        $err = new Error();
        if(is_null($condition)){
            $err->setError(Errors::FAILED);
            $err->setData('请添加查询条件');
            return response()->json($err);
        }
        $data = $model::getByCondition($condition);
        if($data->isEmpty()){
            $err->setError(Errors::NOT_FOUND);
            $err->setData('未查询到相关数据');
        }else{
            $err->setData($data);
        }
        return response()->json($err);
    }
}
